package com.lemon.smartsafety.ui.activity.fire.item.situation;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.RefreshViewEntity;
import com.lemon.smartsafety.entity.tab.TabEntity;
import com.lemon.smartsafety.result.fire.FireListMode;
import com.lemon.smartsafety.result.fire.SuperviseCompanyResult;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.ui.adapter.ViewPagerAdapter;
import com.lemon.smartsafety.ui.fragment.fire.situation.DeviceSFragment;
import com.lemon.smartsafety.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class DeviceSituationActivity extends NewBaseActivity {

    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.tl_com)
    CommonTabLayout tlCom;
    @Bind(R.id.viewPager)
    ViewPager viewPager;



    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private String[] mTitles = {"全部", "完好", "故障","未检查"};

    private FragmentPagerAdapter fragmentPagerAdapter;
    private List<Fragment> list;
    private DeviceSFragment deviceSFragmentAll;
    private DeviceSFragment deviceSFragmentTrue;
    private DeviceSFragment deviceSFragmentFalse;
    private DeviceSFragment deviceSFragmentNone;

    private SuperviseCompanyResult.ResultEntity resultEntity;
    private FireListMode fireListMode;
    private String check_type = "";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getBundleArgs() {
        if (getIntent().getExtras() != null) {
            resultEntity = (SuperviseCompanyResult.ResultEntity) getIntent().getSerializableExtra(BundleKey.FIRE_COMPANY_BASE_INFO);
            fireListMode = (FireListMode) getIntent().getSerializableExtra(BundleKey.FIRE_PERSONAL_DATA);
            check_type = getIntent().getStringExtra(BundleKey.FIRE_CHECK_TYPE);
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_device_situation;
    }

    @Override
    protected void initTitleView() {
        setBarTitle("设施完好详情");

        try {
            setBarTitle(fireListMode.getTypeName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initView() {
        initTab();
    }


    private void initTab() {
        list = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }


        for (int i = 0; i < mTitles.length; i++) {
            switch (i) {
                case 0:
                    bundle = new Bundle();
                    bundle.putString(BundleKey.FIRE_CHECK_TYPE, check_type);
                    bundle.putSerializable(BundleKey.FIRE_COMPANY_BASE_INFO, resultEntity);
                    bundle.putInt(BundleKey.FIRE_PERSONAL_S, i);
                    bundle.putSerializable(BundleKey.FIRE_PERSONAL_DATA, fireListMode);
                    deviceSFragmentAll = new DeviceSFragment();
                    deviceSFragmentAll.setArguments(bundle);
                    list.add(deviceSFragmentAll);
                    break;
                case 1:
                    bundle = new Bundle();
                    bundle.putString(BundleKey.FIRE_CHECK_TYPE, check_type);
                    bundle.putSerializable(BundleKey.FIRE_COMPANY_BASE_INFO, resultEntity);
                    bundle.putInt(BundleKey.FIRE_PERSONAL_S, i);
                    bundle.putSerializable(BundleKey.FIRE_PERSONAL_DATA, fireListMode);
                    deviceSFragmentTrue = new DeviceSFragment();
                    deviceSFragmentTrue.setArguments(bundle);
                    list.add(deviceSFragmentTrue);
                    break;
                case 2:
                    bundle = new Bundle();
                    bundle.putString(BundleKey.FIRE_CHECK_TYPE, check_type);
                    bundle.putSerializable(BundleKey.FIRE_COMPANY_BASE_INFO, resultEntity);
                    bundle.putInt(BundleKey.FIRE_PERSONAL_S, i);
                    bundle.putSerializable(BundleKey.FIRE_PERSONAL_DATA, fireListMode);
                    deviceSFragmentFalse = new DeviceSFragment();
                    deviceSFragmentFalse.setArguments(bundle);
                    list.add(deviceSFragmentFalse);
                    break;
                case 3:
                    bundle = new Bundle();
                    bundle.putString(BundleKey.FIRE_CHECK_TYPE, check_type);
                    bundle.putSerializable(BundleKey.FIRE_COMPANY_BASE_INFO, resultEntity);
                    bundle.putInt(BundleKey.FIRE_PERSONAL_S, i);
                    bundle.putSerializable(BundleKey.FIRE_PERSONAL_DATA, fireListMode);
                    deviceSFragmentNone = new DeviceSFragment();
                    deviceSFragmentNone.setArguments(bundle);
                    list.add(deviceSFragmentNone);
                    break;
            }
        }

        viewPager.setOffscreenPageLimit(mTitles.length + 1);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentPagerAdapter = new ViewPagerAdapter(fragmentManager, list);
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                LogUtils.LOGE("Viewpage选择了:" + String.valueOf(i));
                tlCom.setCurrentTab(i);
                EventBus.getDefault().post(new RefreshViewEntity(DeviceSFragment.class.getName(), String.valueOf(i)));
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        tlCom.setTabData(mTabEntities);
        tlCom.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position, false);
            }

            @Override
            public void onTabReselect(int position) {
//                if (position == 0) {
//                    mTabs.showMsg(0, new Random().nextInt(100) + 1);
//                }
            }
        });


    }

    @Override
    protected void initHttp() {

    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {

    }

    @Override
    protected void callFinish() {
        mActivity.finish();
    }
}
